<template>
	<view class="height-screen">
		<cu-custom isBack bgColor="bg-white">
			<block slot="content">{{$t('address.add')}}</block>
		</cu-custom>
		<view class="bg-white padding-lr radius-xs margin-ht" style="margin-top: 20rpx;">
			<u-form labelPosition="left" labelWidth="120">
				<u-form-item :label="$t('address.people')" borderBottom>
					<u-input v-model="form.name" border="none" :placeholder="$t('public.placeholderContent')"
						inputAlign="right"></u-input>
				</u-form-item>
				<u-form-item :label="$t('address.phone')" borderBottom>
					<u-input v-model="form.phone" border="none" type="number"
						:placeholder="$t('public.placeholderContent')" inputAlign="right"></u-input>
				</u-form-item>
				<!-- <u-form-item :label="$t('address.city')" borderBottom>
					<u-input v-model="form.city" border="none" :placeholder="$t('public.placeholderContent')"
						inputAlign="right"></u-input>
				</u-form-item> -->
				<u-form-item :label="$t('address.address')">
					<u-input v-model="form.address" border="none" :placeholder="$t('public.placeholderContent')"
						inputAlign="right"></u-input>
				</u-form-item>
			</u-form>
		</view>
		<view style="position: fixed;bottom: 60rpx;left: 25rpx;right: 25rpx;">
			<view class="setButton" style="height: 90rpx;background: #06C168;border-radius: 40rpx;"
				@tap.stop="isEdit?addressEdit():addressAdd()">
				<text class="font-400 text-df text-white">{{$t('public.release')}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		addressAdd,
		addressEdit,
		addressInfo
	} from '@/apis'
	export default {
		data() {
			return {
				form: {
					name: '',
					phone: '',
					city: '',
					address: '',
					default: 0
				},
				isEdit: false,
				editId: ''
			}
		},
		onLoad(options) {
			if (options.isEdit == 1) {
				this.isEdit = true
				this.editId = options.id
				this.addressInfo()
			}
		},
		methods: {
			async addressAdd() {
				console.log("this.$t('address.address')",this.$t('address.address'))
				await this.helper.checkForm([
					['string', this.form.name, this.$t('address.people')],
					['string', this.form.phone, this.$t('address.phone')],
					// ['string', this.form.city, this.$t('address.city')],
					['string', this.form.address, this.$t('address.address')]
				])
				await addressAdd({
					name: this.form.name,
					phone: this.form.phone,
					// city: this.form.city,
					address: this.form.address,
					default: this.form.default,
				})
				this.helper.showToast(this.$t('message.success'))
				await this.helper.delay(1000, () => {
					uni.navigateBack()
				})
			},
			async addressInfo() {
				const res = await addressInfo({
					id: this.editId
				})
				this.form = {
					name: res.name,
					phone: res.phone,
					city: res.city,
					address: res.address,
					default: res.default
				}
			},
			async addressEdit() {
				await this.helper.checkForm([
					['string', this.form.name, this.$t('address.people')],
					['string', this.form.phone, this.$t('address.phone')],
					// ['string', this.form.city, this.$t('address.city')],
					['string', this.form.address, this.$t('address.address')]
				])
				await addressEdit({
					name: this.form.name,
					phone: this.form.phone,
					// city: this.form.city,
					address: this.form.address,
					default: this.form.default,
					id: this.editId
				})
				this.helper.showToast(this.$t('message.success'))
				await this.helper.delay(1000, () => {
					uni.navigateBack()
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
</style>